package cn.edu.neusoft.neufooddemo.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import cn.edu.neusoft.neufooddemo.activity.MyCartActivity;
import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.activity.AlterUserInfoActivity;
import cn.edu.neusoft.neufooddemo.activity.MyCommentActivity;
import cn.edu.neusoft.neufooddemo.activity.MyOrderActivity;
import cn.edu.neusoft.neufooddemo.activity.SettingActivity;
import cn.edu.neusoft.neufooddemo.bean.UserInfo;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.UserModel;
import de.hdodenhof.circleimageview.CircleImageView;


public class MeFragment extends BaseFragment implements RetrofitListener<UserInfo>{

    private ImageView my_setting;
    private CircleImageView user_photo;
    private TextView my_username, my_telephone;
    private LinearLayout my_userinfo,my_cart, my_order, my_comment;
    private UserInfo userInfo;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setLayout_file(R.layout.tab04);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    void initViews() {
        my_username = (TextView) view.findViewById(R.id.my_username);
        my_telephone = (TextView) view.findViewById(R.id.my_telephone);
        my_setting = (ImageView) view.findViewById(R.id.my_setting);
        my_userinfo = (LinearLayout) view.findViewById(R.id.my_userinfo);
        my_order = (LinearLayout) view.findViewById(R.id.my_order);
        my_comment = (LinearLayout) view.findViewById(R.id.my_comment);
        my_cart = (LinearLayout) view.findViewById(R.id.my_cart);
        user_photo=(CircleImageView)view.findViewById(R.id.user_photo);
    }

    void initEvents() {
        my_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
            }
        });
        my_userinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AlterUserInfoActivity.class);
                intent.putExtra("username", userInfo.getUsername());
                intent.putExtra("password", userInfo.getUserpass());
                intent.putExtra("telephone", userInfo.getMobilenum());
                intent.putExtra("adress", userInfo.getAddress());
                startActivity(intent);
            }
        });
        my_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCartActivity.class);
                startActivity(intent);
            }
        });
        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent);
            }
        });
        my_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCommentActivity.class);
                startActivity(intent);
            }
        });
    }

    void initData() {
        SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String srcPath=sp.getString("srcPath","");
        if(!"".equals(srcPath))
        {
            setFileToImage(srcPath,user_photo);
        }
        UserModel userModel = new UserModel();
        userModel.getUserById(getUser_id(),this);
    }

    @Override
    public void onSuccess(UserInfo userInfo,int flag) {
        this.userInfo=userInfo;
        my_username.setText(userInfo.getUsername());
        my_telephone.setText(userInfo.getMobilenum());
    }

    @Override
    public void onFail() {
        showToast("网络发生错误");
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
    void setFileToImage(String srcPath,ImageView imageView)
    {
        try {
            FileInputStream f = new FileInputStream(srcPath);
            Bitmap bm = null;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;//图片的长宽都是原来的1/8
            BufferedInputStream bis = new BufferedInputStream(f);
            bm = BitmapFactory.decodeStream(bis, null, options);
            imageView.setImageBitmap(bm);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
